package com.ds.web.controller.applet;

import com.ds.applet.domain.test;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;


@RestController
@RequestMapping("/getData")
public class pc {
    //测试接口
    @RequestMapping("/getCard")
    public String getCard(@RequestParam String url) throws IOException {
        Document document = Jsoup.connect(url).get();
        Elements elements = document.getElementsByClass("car-name");
        return elements.text().toString();
    }

//    public static void main(String[] args) throws IOException {
//        getCardIcon();
//    }

//    @RequestMapping("/getCardIcon")
//    public static String getCardIcon() throws IOException {
//        Document document = Jsoup.connect("https://price.pcauto.com.cn").get();
//        Elements elements = document.getElementsByClass("js-letter");
//        ArrayList<String> List = new ArrayList<>();
//        for (int i = 0; i < 24; i++) {
//            if (i < 24) {
//                Elements letter = elements.get(i).getElementsByClass("brand-name");
////                System.out.println(letter.text());
//                if (letter.text() == null || letter.text() == "") {
//                    List.add("1");
//                } else {
//                    List.add(letter.text());
//                }
//
//            }
//        }
//        System.out.println(List.size());
//        System.out.println(List);
//        return "hrllo";
//    }


    public static String getCardIcon() throws IOException {
        Document document = Jsoup.connect("https://www.autohome.com.cn/car/").get();
        String[] Arr = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        for (int j = 0; j < Arr.length; j++) {
            String key = "box" + Arr[j];
            String value = "html" + Arr[j];
            System.out.println(key + "===" + value);
            Element elements = document.getElementById(key).getElementById(value);
            for (int i = 0; i < 35; i++) {
                if (elements.getElementsByTag("dt").text() != "") {
                    // 获取品牌
                    System.out.println(elements.getElementsByTag("dt").get(i).getElementsByTag("div").text() + "///");
                    // 获取车型
                    System.out.println(elements.getElementsByTag("dd").get(i).getElementsByTag("h4").text());
                }
            }
        }
        return "hrllo";
    }


    public static void main(String[] args) {
        // 要写入的数组
        String[] array = {
                "安凯Q5",
                "安凯快乐运"
        };

        // 创建一个新的工作簿
        Workbook workbook = new XSSFWorkbook();
        // 创建一个工作表
        Sheet sheet = workbook.createSheet("汽车型号");

        // 遍历数组，将内容写入工作表
        for (int i = 0; i < array.length; i++) {
            Row row = sheet.createRow(i);
            Cell cell = row.createCell(0);
            cell.setCellValue(array[i]);
        }

        // 将工作簿写入文件系统
        try (FileOutputStream outputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\小程序文件\\汽车型号安凯客车.xlsx")) {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 测试添加汽车型号
     */
    public int addBrandModel(test test) {
        String id = test.getId();//品牌id
        for (String model : test.getList()){//循环获取汽车型号

        }
        return 1;
    }
}

